#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Windows.Forms;
using ZO.SmartCore.Windows.Forms.Design;
using ArgumentException= ZO.SmartCore.Core.ArgumentException;

#endregion

namespace ZO.SmartCore.Windows.Forms
{
    /// <summary>
    /// A content page designed to hold other controls to be shown in the wizard. 
    /// </summary>
    [Designer(typeof(SmartWizardPageDesigner))]
    [ToolboxItem(false)]
    [DefaultEvent("Load")]
    public class SmartWizardPage : Panel
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartWizardPage"/> class.
        /// </summary>
        public SmartWizardPage() : base()
        {
            InitializeComponent();

            this.VisibleChanged += new EventHandler(SmartWizardPage_VisibleChanged);

        }

        /// <summary>
        /// Handles the VisibleChanged event of the SmartWizardPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void SmartWizardPage_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                if (this.Wizard.SelectedPage == this)
                {
                    this.OnLoad();
                }
            }
        }


        #endregion

        #region Destructor

        #endregion

        #region Fields
        private bool _AllowCancel = true;
        private bool _AllowMoveNext = true;
        private bool _AllowMoveBack = true;
        private SmartWizardPage _NextPage;
        private SmartWizardPage _PreviousPage;

        #endregion

        #region Events

        /// <summary>
        /// Occurs before the control becomes visible. 
        /// </summary>
        public event EventHandler Load;


        /// <summary>
        /// Occurs after the page is displayed.
        /// </summary>
        /// <remarks>
        /// <para>This event is only raised when moving foward, as it is assumed that when moving backward the page will need no configuration.</para>
        /// </remarks>
        public event EventHandler AfterDisplay;
 
        /// <summary>
        /// Occurs before the page is displayed.
        /// </summary>
        /// <remarks>
        /// <para>This event is only raised when moving forward, as it is assumed that when moving backward the page will need no configuration.</para>
        /// </remarks>
        public event EventHandler BeforeDisplay;
 
        /// <summary>
        /// Occurs when the user presses the Back button.
        /// </summary>
        /// <remarks>
        /// <para>This event gives the developer the opportunity to validate the data entered on the page, and cancel the move if necessary.</para>
        /// </remarks>
        public event EventHandler<CancelEventArgs> BeforeMoveBack;

        /// <summary>
        /// Occurs when the user presses the Next button.
        /// </summary>
        /// <remarks>
        /// <para>This event gives the developer the opportunity to validate the data entered on the page, and cancel the move if necessary.</para>
        /// </remarks>
        public event EventHandler<CancelEventArgs> BeforeMoveNext;

        #endregion

        #region Operators

        #endregion

        #region Properties

        /// <summary>
        /// Indicates whether the user will be allowed to cancel the wizard from this page.
        /// </summary>
        [Category("Wizard"), Description("Indicates whether the user will be allowed to cancel the wizard from this page."), DefaultValue(true)]
        [RefreshProperties(RefreshProperties.All)]
        public bool AllowCancel
        {
            get
            {
                return this._AllowCancel;
            }
            set
            {
                this._AllowCancel = value;
                if (this.IsCurrent)
                {
                    ((SmartWizard)Parent).ActivateButtons();
                }
            }
        }


        /// <summary>
        /// Indicates whether the user will be allowed to move forwards from this page.
        /// </summary>
        [Description("Indicates whether the user will be allowed to move forwards from this page."), 
        Category("Wizard"), DefaultValue(true)]
        [RefreshProperties(RefreshProperties.All)]
        public bool AllowMoveNext
        {
            get
            {
                return this._AllowMoveNext;
            }
            set
            {
                this._AllowMoveNext = value;
                if (this.IsCurrent)
                {
                    ((SmartWizard)Parent).ActivateButtons();
                }
            }
        }

        /// <summary>
        /// Indicates whether the user will be allowed to move backwards from the page.
        /// </summary>
        [DefaultValue(true), Category("Wizard"), 
        Description("Indicates whether the user will be allowed to move backwards from the page.")]
        [RefreshProperties(RefreshProperties.All)]
        public bool AllowMoveBack
        {
            get
            {
                return this._AllowMoveBack;
            }
            set
            {
                this._AllowMoveBack = value;
                if (this.IsCurrent)
                {
                    ((SmartWizard)Parent).ActivateButtons();
                }
            }
        }
 



        /// <summary>
        /// Indicates whether the page is the selected page within the wizard.
        /// </summary>
        [Browsable(false)]
        [RefreshProperties(RefreshProperties.All)]
        public bool IsCurrent
        {
            get
            {
                if (Parent is SmartWizard)
                {
                    return (((SmartWizard)Parent).SelectedPage == this);
                }
                return false;
            }
        }



        /// <summary>
        /// The page to move to when the user presses the Next button.
        /// </summary>
        [Description("The page to move to when the user presses the Next button.")]
        [DefaultValue(typeof(SmartWizardPage), null), Category("Wizard")]
        [RefreshProperties(RefreshProperties.All)]
        public SmartWizardPage NextPage
        {
            get
            {
                return this._NextPage;
            }
            set
            {
                if (value == this)
                {
                    throw new ArgumentException("Cannot navigate from one page to the same page.");
                }
                this._NextPage = value;
                if (this.IsCurrent)
                {
                    ((SmartWizard)Parent).ActivateButtons();
                }
            }
        }

        /// <summary>
        /// The page to move to when the user presses the Back button.
        /// </summary>
        [Description("The page to move to when the user presses the Back button.")]
        [DefaultValue(typeof(SmartWizardPage), null), Category("Wizard")]
        [RefreshProperties(RefreshProperties.All)]
        public SmartWizardPage PreviousPage
        {
            get
            {
                return this._PreviousPage;
            }
            set
            {
                if (value == this)
                {
                    throw new ArgumentException("Cannot navigate from one page to the same page.");
                }
                this._PreviousPage = value;
                if (this.IsCurrent)
                {
                    ((SmartWizard)Parent).ActivateButtons();
                }
            }
        }
 


        /// <summary>
        /// The wizard to which the page belongs.
        /// </summary>
        [Browsable(false)]
        public SmartWizard Wizard
        {
            get
            {
                return (Parent as SmartWizard);
            }
        }
 

        #endregion

        #region Methods

        /// <summary>
        /// Raises the <see cref="Load"/> event. 
        /// </summary>
        protected internal virtual void OnLoad()
        {
            if (Load != null)
            {
                this.Load(this, EventArgs.Empty);
            }
        }

        //protected override DisplayNameAttribute


        /// <summary>
        /// Raises the AfterDisplay event.
        /// </summary>
        protected internal virtual void OnAfterDisplay()
        {
            if (this.AfterDisplay != null)
            {
                this.AfterDisplay(this, EventArgs.Empty);
            }
        }


        /// <summary>
        /// Raises the BeforeDisplay event.
        /// </summary>
        protected internal virtual void OnBeforeDisplay()
        {
            if (this.BeforeDisplay != null)
            {
                this.BeforeDisplay(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Raises the BeforeMoveBack event.
        /// </summary>
        /// <param name="e">Arguments for the event handling procedure.</param>
        protected internal virtual void OnBeforeMoveBack(CancelEventArgs e)
        {
            if (this.BeforeMoveBack != null)
            {
                this.BeforeMoveBack(this, e);
            }
        }


        /// <summary>
        /// Raises the BeforeMoveNext event.
        /// </summary>
        /// <param name="e">Arguments for the event handling procedure.</param>
        protected internal virtual void OnBeforeMoveNext(CancelEventArgs e)
        {
            if (this.BeforeMoveNext != null)
            {
                this.BeforeMoveNext(this, e);
            }
        }


        /// <summary>
        /// Activates this instance.
        /// </summary>
        internal void Activate()
        {
            this.SelectNextControl(null, true, true, true, true);
        }



        #endregion

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // SmartWizardPage
            // 
            this.ResumeLayout(false);

        }
    }
}
